//
//  ZVideoViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/21.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZVideoViewController.h"
#import "ZVideoCollectionViewCell.h"
#import "ZPlayVideoViewController.h"
#import "ZDVideoModel.h"
static NSString * const ZVideoViewIdentifier = @"ZVideoViewIdentifier";
static int vPage = 1;
@interface ZVideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * mainCollectionView;
@property (nonatomic,strong) NSMutableArray <ZDVideoModel *> * dVideoArray;
@end

@implementation ZVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self initlizeInterface];
}
- (void)initialzeDatas {
    self.dVideoArray = [@[] mutableCopy];
}
- (void)initlizeInterface {
    self.view.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView .mj_header =  [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self videoNetWorking];
        [self.mainCollectionView.mj_header endRefreshing];
    }];
    // 上拉加载
    self.mainCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self moreVidepNetWorking];
        [self.mainCollectionView.mj_footer endRefreshing];
    }];

    [self.view addSubview:self.mainCollectionView];
    [self videoNetWorking];
}

- (void)videoNetWorking {
    vPage = 1;
    NSDictionary * dict = @{@"did":self.did,@"P":[NSString stringWithFormat:@"%d",vPage]};
    [YJProgressHUD showProgress:@"正在刷新" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/video" sucessBlock:^(id content) {
        NSDictionary * vDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",vDict);
        if ([vDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [self handleDataWithArray:vDict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"请求出错" inView:self.view];
        }

    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求出错" inView:self.view];
    }];
}
- (void)moreVidepNetWorking {
    vPage++;
    NSDictionary * dict = @{@"did":self.did,@"P":[NSString stringWithFormat:@"%d",vPage]};
    [YJProgressHUD showProgress:@"正在刷新" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/video" sucessBlock:^(id content) {
        NSDictionary * vDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",vDict);
        if ([vDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [self moreHandleDataWithArray:vDict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"请求出错" inView:self.view];
        }
        
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求出错" inView:self.view];
    }];

}
- (void)handleDataWithArray:(NSArray *)array {
    
    [self.mainCollectionView.mj_footer resetNoMoreData];
    [self.dVideoArray removeAllObjects];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [self.mainCollectionView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [self.mainCollectionView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        ZDVideoModel * model = [[ZDVideoModel alloc] initWithDict:dict];
        [self.dVideoArray addObject:model];
    }
    [self.mainCollectionView reloadData];
    
}
- (void)moreHandleDataWithArray:(NSArray *)array {
    [self.mainCollectionView.mj_footer resetNoMoreData];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [self.mainCollectionView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [self.mainCollectionView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        ZDVideoModel * model = [[ZDVideoModel alloc] initWithDict:dict];
        [self.dVideoArray addObject:model];
    }
    [self.mainCollectionView reloadData];
}
// 集合视图协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return (self.dVideoArray&&self.dVideoArray.count != 0)? self.dVideoArray.count : 0 ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZVideoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZVideoViewIdentifier forIndexPath:indexPath];
    cell.dModel = self.dVideoArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self jumpVoideoWithRow:indexPath.row];
}

- (void)jumpVoideoWithRow:(NSInteger)row {
    ZPlayVideoViewController * ZPVVC = [[ZPlayVideoViewController alloc] init];
    ZPVVC.vid = self.dVideoArray[row].vid;
    [self.navigationController pushViewController:ZPVVC animated:YES];
}
// lazy
- (UICollectionView *)mainCollectionView {
    if (_mainCollectionView) {
        return _mainCollectionView;
    }
    UICollectionViewFlowLayout * videoLayout=[[UICollectionViewFlowLayout alloc] init];
    videoLayout.minimumLineSpacing = 10;
    videoLayout.itemSize = CGSizeMake(140 *xsIZE,130*ysIZE);
    videoLayout.sectionInset = UIEdgeInsetsMake(10*xsIZE, 10*xsIZE, 10*xsIZE, 10*xsIZE);
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height) collectionViewLayout:videoLayout];
    _mainCollectionView.backgroundColor = KHColorFromRGB(0xf8f8f8f8);
    _mainCollectionView.showsVerticalScrollIndicator = YES;
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    [_mainCollectionView registerClass:[ZVideoCollectionViewCell class] forCellWithReuseIdentifier:ZVideoViewIdentifier];
    return _mainCollectionView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
